package com.itzz.spring_zhoneHeng.controller;

import com.itzz.spring_zhoneHeng.base.RestResponse;
import com.itzz.spring_zhoneHeng.dao.ModuleDao;
import com.itzz.spring_zhoneHeng.pojo.Module;
import com.itzz.spring_zhoneHeng.pojo.Role;
import com.itzz.spring_zhoneHeng.service.ModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/module")
public class ModuleController {

    @Autowired
    ModuleService moduleService;

    @Autowired
    ModuleDao moduleDao;

    @RequestMapping("/queryModuleAll")
    public RestResponse queryModuleAll(){
        List<Module> modules = moduleDao.selectList(null);
        return RestResponse.ok(modules);
    }


    //查询所有的权限需要添加 一个角色的权限
    @RequestMapping("/queryAll")
    public RestResponse module(@RequestBody HashMap<String,Object> map){
        RestResponse restResponse = moduleService.queryAll(map);
        return restResponse;
    }

    //删除权限 需要管理员权限
    @RequestMapping("/del/{id}")
    @PreAuthorize("hasRole('ROLE_super')")
    public RestResponse del(@PathVariable("id") Integer id){
        System.out.println(id);
        int i = moduleDao.deleteById(id);
        return RestResponse.ok(i);
    }



    //回显数据
    @RequestMapping("/queryById/{id}")
    public RestResponse queryById(@PathVariable("id") Integer id){
        Module module = moduleDao.selectById(id);
        return RestResponse.ok(module);
    }

    //修改数据   获取不是hashmap的数据 就直接返回执行
    @RequestMapping("/update")
    @PreAuthorize("hasRole('ROLE_super')")
    public RestResponse update(Module module){
        int i = moduleDao.updateById(module);
        return RestResponse.ok(i);
    }


    //添加数据
    @RequestMapping("/add")
    @PreAuthorize("hasRole('ROLE_super')")
    public RestResponse add(Module module){
        RestResponse add = moduleService.add(module);

        return add;
    }
}
